package fractal;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import astar.AStar;

import entity.Position;

public class GenerateRiver2
{
	Randomiseur random = new Pseudoalea("RIV");
	
	public void generateRivers( BiomeMap pMap, int pNbRiver )
	{
		for( int i = 0; i< pNbRiver;i++)
		{
			generateRiver( pMap );
		}
	}
	
	public void generateRiver( BiomeMap pMap )
	{
		
		List<Position> listMountain = searchBiome( pMap , BiomeType.MOUNTAIN );
		List<Position> listSea = searchBiome( pMap , BiomeType.SEA );
		int alea = (int)(random.random() * (double)listMountain.size()); 
		Position dep = listMountain.get(  alea );
		alea = (int)(random.random() * (double)listSea.size()); 
		Position arr = listSea.get( alea );
		 List<Position> riverPos = new AStar(pMap).searchPath( dep , arr );
		 for(Position pos : riverPos)
		 {
			 pMap.getMap()[pos.getX()][pos.getY()].setRiver( true );
		 } 

	}

	private List<Position> searchBiome( BiomeMap pMap , BiomeType pBiomeType )
	{
		List<Position> result = new ArrayList<Position>();
		for ( int i = 0; i < pMap.getMap().length; i++ )
		{
			for ( int j = 0; j < pMap.getMap().length; j++ )
			{
				TerrainBiome terBio = pMap.getMap()[i][j];
				if ( pBiomeType.equals( terBio.getBiomeType() ) )
				{
					result.add( new Position( i, j ) );
				}
			}
		}
		return result;
	}
}
